﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using WebMatrix.WebData;
using Lapkin.Models.Repositories;

namespace Lapkin.Authenticate
{
    public class SiteMembershipProvider : ExtendedMembershipProvider
    {
        private readonly SqlRepository _repository = new SqlRepository();

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer,
                                                  bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            status = MembershipCreateStatus.ProviderError;
            return null;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion,
                                                             string newPasswordAnswer)
        {
            return false;
        }

        public override string GetPassword(string username, string answer)
        {
            return null;
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return false;
        }

        public override string ResetPassword(string username, string answer)
        {
            return null;
        }

        public override void UpdateUser(MembershipUser user)
        {
            
        }

        public override bool ValidateUser(string username, string password)
        {
            return _repository.AccountRepository.ValidateUser(username, password);
        }

        public override bool UnlockUser(string userName)
        {
            return false;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            Guid uid;
            if (Guid.TryParse(providerUserKey.ToString(), out uid))
            {
                var user = _repository.AccountRepository.GetUser(u => u.Id == uid);
                return user != null
                           ? new MembershipUser("SiteMembershipProvider", user.Email, user.Id, user.Email, "", "", true,
                                                user.IsBanned?? false, user.RegDate, user.LastActiveDate, user.LastActiveDate,
                                                DateTime.Now, DateTime.Now)
                           : null;
            }
            return null;
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            var user = _repository.AccountRepository.GetUser(u => u.Email == username);
            return user != null
                       ? new MembershipUser("SiteMembershipProvider", user.Email, user.Id, user.Email, "", "", true,
                                            user.IsBanned ?? false, user.RegDate, user.LastActiveDate, user.LastActiveDate,
                                            DateTime.Now, DateTime.Now)
                       : null;
        }

        public override string GetUserNameByEmail(string email)
        {
            return email;
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return false;
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 0;
            return null;
        }

        public override int GetNumberOfUsersOnline()
        {
            return 0;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 0;
            return null;
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 0;
            return null;
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false;  }
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override string ApplicationName { get; set; }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 3; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 3; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Clear;}
        }

        public override int MinRequiredPasswordLength
        {
            get { return 6; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 6; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return ""; }
        }

        public override ICollection<OAuthAccountData> GetAccountsForUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary<string, object> values)
        {
            throw new NotImplementedException();
        }

        public override string CreateAccount(string userName, string password, bool requireConfirmationToken)
        {
            throw new NotImplementedException();
        }

        public override bool ConfirmAccount(string userName, string accountConfirmationToken)
        {
            throw new NotImplementedException();
        }

        public override bool ConfirmAccount(string accountConfirmationToken)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteAccount(string userName)
        {
            throw new NotImplementedException();
        }

        public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow)
        {
            throw new NotImplementedException();
        }

        public override int GetUserIdFromPasswordResetToken(string token)
        {
            throw new NotImplementedException();
        }

        public override bool IsConfirmed(string userName)
        {
            throw new NotImplementedException();
        }

        public override bool ResetPasswordWithToken(string token, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override int GetPasswordFailuresSinceLastSuccess(string userName)
        {
            throw new NotImplementedException();
        }

        public override DateTime GetCreateDate(string userName)
        {
            throw new NotImplementedException();
        }

        public override DateTime GetPasswordChangedDate(string userName)
        {
            throw new NotImplementedException();
        }

        public override DateTime GetLastPasswordFailureDate(string userName)
        {
            throw new NotImplementedException();
        }
    }
}